package getstart;

import com.blackhorse.annotation.dao.AUserMapper;
import com.blackhorse.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 注解配置
 */
public class GetStart_Annotation {
    public static void main(String[] args) {
        SqlSession sqlSession = null;
        try {
            //1.读配置文件
            InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.创建SqlSessionFactory工厂
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = builder.build(is);
            //3.使用工厂生产SqlSession
            sqlSession = sqlSessionFactory.openSession();
            //4.获取Dao的代理对象mapper
            AUserMapper mapper = sqlSession.getMapper(AUserMapper.class);
            //5.执行操作
            List<User> users = mapper.findAll();
            for (User user : users) {
                System.out.println(user);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //6.释放资源
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}
